/**
  ******************************************************************************
  * @file    lin.h
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   Header file of lin.c.
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __LIN_H
#define __LIN_H

#ifdef __cplusplus
extern "C" {
#endif


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "sym32l010_hal_def.h"
#include "sym32l010_hal_uart.h"
#include "sym32l010_hal_systick.h"


/******************************************************************************/
/* Global type definitions                                                    */
/******************************************************************************/
typedef enum
{
    LIN_ERR_NONE      = 0,
    LIN_ERR_RECVDATA  = 1,
    LIN_ERR_RECVSUM   = 2,
    LIN_ERR_CHECKSUM  = 3,
    LIN_ERR_COMPARE   = 4
} LIN_ErrCodeTypeDef;

typedef struct
{
    uint8_t PId;
    uint8_t State;
    uint8_t Count;
    uint8_t TxTotalLen;
    uint8_t RxTotalLen;
    uint8_t *pTxBuff;
    uint8_t *pRxBuff;
} LIN_SlaveClassTypeDef;

typedef enum
{
    LIN_USER_PID_0           = 0x00U,
    LIN_USER_PID_1           = 0x01U,
    LIN_USER_PID_2           = 0x02U,
    LIN_USER_PID_3           = 0x03U,
    LIN_USER_PID_4           = 0x04U,
    LIN_USER_PID_5           = 0x05U,
    LIN_USER_PID_RXDATA      = 0x85U,
    LIN_USER_PID_TXDATA      = 0xD6U,
    LIN_USER_PID_59          = 0x3BU,
} LIN_UserPidTypeDef;


typedef enum
{
    LIN_ERROR_MASK           =  0x0FU,
    LIN_STATE_MASK           =  0xF0U,
    LIN_STATE_IDLE           =  0x00U,
    LIN_STATE_SYNC           =  0x10U,
    LIN_STATE_RXPID          =  0x20U,
    LIN_STATE_RXDATA         =  0x30U,
    LIN_STATE_RXSUM          =  0x40U,
    LIN_STATE_TXDATA         =  0x50U,
    LIN_STATE_TXSUM          =  0x60U,
    LIN_STATE_TXSUMOVER      =  0x70U,
    LIN_STATE_RXOVER         =  0x80U,
    LIN_STATE_TXOVER         =  0x90U
} LIN_SlaveStateTypeDef;


/******************************************************************************/
/* Global pre-processor symbols/macros ('#define')                            */
/******************************************************************************/
#define DATA_LEN             8
//#define LIN_IRQ_ISR_MASK     (UARTx_ISR_RXBRK_Msk | UARTx_ISR_BAUD_Msk | UARTx_ISR_RC_Msk | UARTx_ISR_TC_Msk)
#define LIN_IRQ_ISR_MASK     (UART_IT_FLAG_RXBRK | UART_IT_FLAG_BAUD | UART_IT_FLAG_RC | UART_IT_FLAG_TC)


/******************************************************************************/
/* Global macro function                                                      */
/******************************************************************************/


/******************************************************************************/
/* Exported variables ('extern', definition in C source)                      */
/******************************************************************************/
extern uint8_t                UserSendBuf[DATA_LEN];
extern uint8_t                UserRecvBuf[DATA_LEN];
extern LIN_SlaveClassTypeDef  hLinSlave;


/******************************************************************************/
/* Global function prototypes ('extern', definition in C source)              */
/******************************************************************************/
uint8_t           LIN_CalcPID(uint8_t IdCode);
uint8_t           LIN_CalcCheckSum(uint8_t *Buff, uint8_t DataLen);

void              LIN_Delay_nBit(UART_HandleTypeDef *hUart, uint8_t TimeOutBit);

void              LIN_SendBreak_Polling(UART_HandleTypeDef *hUart, uint16_t SendBreakLen, uint8_t SpaceBit);
void              LIN_SendSync_Polling(UART_HandleTypeDef *hUart, uint8_t SpaceBit);
void              LIN_SendData_Polling(UART_HandleTypeDef *hUart, uint8_t *pTxBuf, uint16_t TxCnt, uint8_t SpaceBit);

HAL_StatusTypeDef LIN_RecvBreak_Polling(UART_HandleTypeDef *hUart, uint8_t TimeOut);
HAL_StatusTypeDef LIN_RecvSync_Polling(UART_HandleTypeDef *hUart, uint8_t TimeOut);
HAL_StatusTypeDef LIN_RecvData_Polling(UART_HandleTypeDef *hUart, uint8_t *pRxBuf, uint16_t RxCnt, uint8_t TimeOut);

void              LIN_SlaveIT_PID_Process_CallBack(UART_HandleTypeDef *UARTx, LIN_SlaveClassTypeDef *pLinSlave);
void              LIN_SlaveIT_SendFrameOver_CallBack(void);
void              LIN_SlaveIT_RecvFrameOver_CallBack(UART_HandleTypeDef *UARTx, uint8_t *pRecvBuff, uint8_t RecvLength);

void              LIN_SlaveIT_IRQHandlerCallBack(UART_HandleTypeDef *hUart, LIN_SlaveClassTypeDef *pLinSlave);

#ifdef __cplusplus
}
#endif

#endif /* __LIN_H */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
